package paint;

import java.awt.Toolkit.*;
import java.awt.Image.*;
import java.util.*;
import java.awt.datatransfer.Clipboard.*;
import java.awt.*;
import java.awt.Toolkit.*;
import java.awt.image.*;

/** A brightness object is a dialogue box that allows the user to change the brightness of the whole image.
 * The user can then edit the options in the dialog box, and apply the changes to the image.
 * Dialog box is terminated by clicking on the 'OK' or 'Cancel' buttons. OK will instantiated the changes,
 * Cancel will do nothing to the original image.
 * The user can change the brightness of the image to any ratio between 1% and 10000% to the original image.
 * There are no OS/Hardware dependencies and no variances.  There is no need for any
 * security constraints and no references to external specifications.
 *
 * @author  Paint
 * @version 2.0
 */
public class brightness extends javax.swing.JDialog {

    /** Creates new form brightness.
     *
     * This also initializes all the components and calls the parent's default
     * constructor.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param parent This parameter sends which frame is the parent.
     * @param modal This parameter could be a model, true or false.
     */
    public brightness(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     *
     * This method initializes the entire form with buttons and new dialoge
     * boxes, etc.  It also sets up constraints.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     */
    public void initComponents() {
        //	  ok_action = false;
        ok_cancel = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        center = new javax.swing.JPanel();
        brightness_panel = new javax.swing.JPanel();
        //brightness_value = new javax.swing.JTextField();
        brightness_value = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();


        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        ok_cancel.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints1;

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        gridBagConstraints1 = new java.awt.GridBagConstraints();
        ok_cancel.add(ok, gridBagConstraints1);

        cancel.setText("CANCEL");
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        gridBagConstraints1 = new java.awt.GridBagConstraints();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 1;
        ok_cancel.add(cancel, gridBagConstraints1);

        getContentPane().add(ok_cancel, java.awt.BorderLayout.EAST);

        center.setLayout(new java.awt.BorderLayout());

        brightness_panel.setLayout(new java.awt.GridBagLayout());
        java.awt.GridBagConstraints gridBagConstraints2;

        brightness_panel.setBorder(new javax.swing.border.TitledBorder("BRIGHTNESS"));
        //brightness_value.setColumns(10);
        //brightness_value.setText("100");
        brightness_value.setMaximumSize(new java.awt.Dimension(0, 100000));
        /*brightness_value.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
        brightnessActionPerformed(evt);
        }
        });
         */
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 1;
        System.out.println("Init Components: " + brightval);
        brightness_value.setValue(brightval);
        brightness_panel.add(brightness_value, gridBagConstraints2);


        jLabel4.setText("Brightness");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 0;
        gridBagConstraints2.gridy = 1;
        brightness_panel.add(jLabel4, gridBagConstraints2);

        jLabel7.setText("  0%		     100%		200%");
        gridBagConstraints2 = new java.awt.GridBagConstraints();
        gridBagConstraints2.gridx = 1;
        gridBagConstraints2.gridy = 2;
        brightness_panel.add(jLabel7, gridBagConstraints2);



        center.add(brightness_panel, java.awt.BorderLayout.CENTER);


        getContentPane().add(center, java.awt.BorderLayout.CENTER);

        pack();
    }

    /** This method executes if the event is a CANCEL button, then it closes the dialog.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of a mouse action.
     */
    public void cancelActionPerformed(java.awt.event.ActionEvent evt) {
        this.closeDialog(new java.awt.event.WindowEvent(this, 0));
    }

    /** This method executes if the event is a OK button.  Then it executes the performed action.
     * In this case, it will brightness the picture
     * accordingly.  If user selected skew instead of brightness, the picture will
     * basically just expand without the borders of the picture to expand.
     *
     * There are no state transitions and no security constraints or OS dependencies.
     *
     * @param evt The event of a mouse action.
     */
    public void okActionPerformed(java.awt.event.ActionEvent evt) {

        // ming 5.2
        int cur_layer = ((Paint) this.getParent()).center.currentLayer;
        LinkedList cur_action_list = (LinkedList) (((Paint) this.getParent()).action_list.get(cur_layer));
        LinkedList cur_redo_list = (LinkedList) (((Paint) this.getParent()).redo_list.get(cur_layer));

        int temp1 = cur_redo_list.size();
        for (int i = 0; i < temp1; i++) {
            cur_redo_list.removeLast();
        // ming 5.2 end
        }
        if (((Paint) this.getParent()).toolSelectall.getSelected() || ((Paint) this.getParent()).toolSelect.getSelected() || ((Paint) this.getParent()).toolMagicSelect.getSelected()) {
            // ming 5.2
            cur_action_list.add("Region brightness change");
            // ming 5.2 end
            BufferedImage selected;
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                selected = ((Paint) this.getParent()).toolSelect.getCopyImage(((Paint) this.getParent()).center);
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolMagicSelect) {
                selected = ((Paint) this.getParent()).toolMagicSelect.getCopyImage(((Paint) this.getParent()).center);
            } else {
                selected = ((Paint) this.getParent()).toolSelectall.getCopyImage(((Paint) this.getParent()).center);
            }
            // float x = Float.parseFloat(brightness_value.getText());
            float x = brightness_value.getValue() * 2;
            float scaleFactor = x / 100;
            RescaleOp op = new RescaleOp(scaleFactor, 0, null);


            selected = op.filter(selected, null);
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                ((Paint) this.getParent()).toolSelect.setPastedImage(selected, ((Paint) this.getParent()).center, ((Paint) this.getParent()).toolSelect.getStartX(), ((Paint) this.getParent()).toolSelect.getStartY());
            // ming 5.2
            //    ((Paint)this.getParent()).toolSelect.backupImage = ((Paint)this.getParent()).center.getBufferedImage();
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelectall) {
                ((Paint) this.getParent()).toolSelectall.setPastedImage(selected, ((Paint) this.getParent()).center, ((Paint) this.getParent()).toolSelectall.getStartX(), ((Paint) this.getParent()).toolSelectall.getStartY());
            //((Paint)this.getParent()).toolSelectall.backupImage = ((Paint)this.getParent()).center.getBufferedImage();
            // ming 5.2 end
            }
            //     else if (((Paint)this.getParent()).currentTool == ((Paint)this.getParent()).magicSelectTool)
            //	  ((Paint)this.getParent()).magicSelectTool.setPastedImage(selected, ((Paint)this.getParent()).center, ((Paint)this.getParent()).magicSelectTool.getStartX(), ((Paint)this.getParent()).magicSelectTool.getStartY());
            //BufferedImage curImage = ((Paint)this.getParent()).center.getBufferedImage();
            //((Paint)this.getParent()).center.setBufferedImage(selected);//curImage);
            //((Paint)this.getParent()).center.repaint();
            selected.flush();

        } else {
            // ming 5.2
            cur_action_list.add("Image brightness change");
            // ming 5.2 end

            // ming 4.24 end
            BufferedImage im = ((Paint) this.getParent()).center.getBufferedImage();

            //	 float x = Float.parseFloat(brightness_value.getText());
            float x = brightness_value.getValue() * 2;


            float scaleFactor = x / 100;
            RescaleOp op = new RescaleOp(scaleFactor, 0, null);
            im = op.filter(im, null);


            Graphics2D gd = im.createGraphics();

            gd.drawImage(im, 0, 0, null);
            ((Paint) this.getParent()).center.setBufferedImage(im);
            im.flush();
        // ming 4.24
        }
        // ming 4.24 end
        // ming 4.26




        // ming 4.26 end
        ((Paint) this.getParent()).updateUndoList();

        this.closeDialog(new java.awt.event.WindowEvent(this, 0));
        brightval = brightness_value.getValue();
        System.out.println("Brightval: " + brightval);
    //	  ok_action = true;
    }

    /** This method is for brightness changing.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of an action.
     */
    public void brightnessActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
    }

    /** Closes the dialog.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param evt The event of an action.
     */
    public void closeDialog(java.awt.event.WindowEvent evt) {
        setVisible(false);
        dispose();
    }

    /** This method basically creates a new brightness object and applies the image
     * into it.	 If there are arguments, it will take them as well.  It will then
     * display the results onto the main canvas.
     * There are no OS dependencies and variances.  No security constraints or external
     * specifications.
     * @param args The command line arguments
     */
    public static void main(String args[]) {
        new brightness(new javax.swing.JFrame(), true).show();
    }
    // Variables declaration - do not modify
    /** Java swing GUI depicting the OK and CANCEL panel.
     */
    private javax.swing.JPanel ok_cancel;
    /** Java swing GUI depicting the OK button.
     */
    private javax.swing.JButton ok;
    /** Java swing GUI depicting the CANCEL button.
     */
    private javax.swing.JButton cancel;
    /** Java swing GUI depicting the CENTER button.
     */
    private javax.swing.JPanel center;
    /** Java swing GUI depicting the BRIGHTNESS button.
     */
    private javax.swing.JPanel brightness_panel;
    /** Java swing GUI depicting the textfield of the brightness change ratio value.
     */
    // private javax.swing.JTextField brightness_value;
    /** Java swing GUI depicting the brightness change ratio value.
     */
    private javax.swing.JSlider brightness_value;
    /** Java swing GUI depicting the Label 4.
     *
     */
    private javax.swing.JLabel jLabel4;
    /** Java swing GUI depicting the Label 7.
     *
     */
    private javax.swing.JLabel jLabel7;
    /** Static int to represent the slider value.
     *
     */
    private static int brightval = 50;

    // End of variables declaration
    /** The dialog is closed by OK
     */
    // public boolean ok_action;
}
